#include <iostream>
using namespace std;
bool isPrime(int n)
{
    if(n == 1) return false;
    for(int i = 2; i<n; i++)
        if((n%i) == 0)
            return false;
    return true;
}
int sumofPF(int n)
{
    int sum = 0;
    while(n>0)
    {
        int temp = n % 10;
        sum = sum + temp * temp;
        n = n /10;
    }
    return sum;
}
int isHappy(int n)
{
    int history[10000];
    int len = 0;
    int temp = n;
    while(true)
    {
        temp = sumofPF(temp);
        //cout<<temp<<endl;
        if(temp == 1)
        {
            if(isPrime(n))
                return (len+1)*2;
            else
                return len+1;
        }
        else
        {
            for(int i = 0; i<len; i++)
            {
                if(history[i] == temp)
                    return 0;
            }
            history[len] = temp;
            len++;
        }
    }
}

int main()
{
    int begin,end;
    cin>>begin>>end;
    int happy[10001] ={0};
    int selHappy[10001] = {0};
    for(int i = begin; i<=end; i++)
    {
            happy[i] = isHappy(i);
            selHappy[i] = isHappy(i);
    }
    for(int i = begin; i<=end; i++)
    {
        if(happy[i]>0)
        {
           int temp = i;
            while(true)
            {
                temp = sumofPF(temp);
                if(temp ==1)
                    break;
                else
                {
                    if(temp>=begin && temp<=end)
                        selHappy[temp] = 0;
                }
            }
        }
    }
    bool ifFind = false;
    for(int i = begin; i<=end; i++)
        if(selHappy[i] > 0)
        {
           cout<<i<<" "<<selHappy[i]<<endl;
            ifFind = true;
        }
    if(!ifFind)
        cout<<"SAD";
    
}